package com.example.yy.usbcameratest.view;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.example.yy.usbcameratest.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


/**
 * @ProjectName: DriveJudge
 * @Package: safeluck.drive.evaluation.customview
 * @ClassName: SelectDialog
 * @Description: java类作用描述
 * @Author: 李占伟
 * @CreateDate: 2020-04-23 15:03
 * @UpdateUser: 更新者
 * @UpdateDate: 2020-04-23 15:03
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

public class SelectDialog extends DialogFragment implements View.OnClickListener {

    private static final String TAG = "SelectDialog";
    private Button button;
    private RadioGroup rgb;
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int SELECT_NONE = -1;
    private int result = SELECT_NONE;
    private RadioButton rb_1,rb_2;
    private ArrayList<String> stringArrayList;


    @Override
    public View onCreateView( LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        if (getDialog() != null) {
            final Window window = getDialog().getWindow();

            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

                getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialogInterface) {
                        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
                        hideBottomUIMenu();
                    }
                } );
            }
        }
        View view = inflater.inflate(R.layout.layout_select_dlg,container,false);
        Bundle bundle = getArguments();
        if (bundle != null){
            stringArrayList = bundle.getStringArrayList("content");
        }
        initView(view);
        return view;
    }

    private void initView(View view) {
        button = view.findViewById(R.id.btn_sure_);
        rb_1 = view.findViewById(R.id.rb1);
        rb_2 = view.findViewById(R.id.rb2);
        if (stringArrayList != null){
            rb_1.setText(stringArrayList.get(0));
            rb_2.setText(stringArrayList.get(1));
        }
        rgb = view.findViewById(R.id.radiogroub);
        rgb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        Log.i(TAG,"第一个被选中");
                        result = FIRST;
                        break;
                    case R.id.rb2:
                        Log.i(TAG,"第二个被选中");
                        result = SECOND;
                        break;               case R.id.rb3:
                        Log.i(TAG,"第san个被选中");
                        result = THIRD;
                        break;
                    default:break;
                }
            }
        });
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_sure_:
                    if (onSelectedListener != null){
                        onSelectedListener.makeYourChoice(result);
                    }
                dismiss();
                break;
        }
    }

    public interface OnSelectedListener{
        void makeYourChoice(int res);
    }

    private OnSelectedListener onSelectedListener;

    public void setSelectedListener(OnSelectedListener onSelectedListener){
        this.onSelectedListener = onSelectedListener;
    }

    public static SelectDialog newInstance(String... args){

        SelectDialog sle = new SelectDialog();
        if (args != null && args.length>=2){
            List<String> strs = Arrays.asList(args);
            ArrayList<String> list = new ArrayList<>(strs);
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("content",list);
            sle.setArguments(bundle);
        }


        return sle;
    }
    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getDialog().getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getDialog().getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    public void dismiss() {

        super.dismiss();
    }
}
